package com.atguigu.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.ConstantPropertiesUtils;
import com.atguigu.yygh.vo.msm.MsmVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName MsmServiceImpl
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/18 19:32
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/18 19:32
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@Service
public class MsmServiceImpl implements MsmService {
    /**
     * 调用阿里云短信接口发送短信
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    @Override
    public boolean send(String phone, String code) {

        //判断手机号是否合法
        if(!isValidChinaPhoneNumber(phone) || StringUtils.isEmpty(phone)){
            return false;
        }


        //调用阿里云短信接口发送短信
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                            ConstantPropertiesUtils.ACCESS_KEY_ID,
                            ConstantPropertiesUtils.SECRECT);

        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //使用https需要设置
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "我学习的尚医通网站");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_468300494");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));


        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //返回true表示发送成功,false表示发送失败
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
    /**
     * 调用阿里云短信接口发送短信
     * @param phone 手机号
     * @param param
     * @return
     */
    private boolean send(String phone, Map<String,Object> param) {

        //判断手机号是否合法
        if(!isValidChinaPhoneNumber(phone) || StringUtils.isEmpty(phone)){
            return false;
        }


        //调用阿里云短信接口发送短信
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);

        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //使用https需要设置
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "我学习的尚医通网站");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_468300494");

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));


        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //返回true表示发送成功,false表示发送失败
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 调用消息队列发送短信
     * @param msmVo 短信参数
     * @return
     */
    @Override
    public boolean sendMq(MsmVo msmVo) {
        //判断手机号
        if(!StringUtils.isEmpty(msmVo.getPhone()) && isValidChinaPhoneNumber(msmVo.getPhone())){
            Boolean isSend=this.send(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }

    // 中国手机号的正则表达式
    private final String CHINA_PHONE_NUMBER_PATTERN = "^1[3-9]\\d{9}$";
    /**
     * 验证手机号是否符合中国手机号的格式
     *
     * @param phoneNumber 待验证的手机号
     * @return 如果是合法的中国手机号，则返回true，否则返回false
     */
    public boolean isValidChinaPhoneNumber(String phoneNumber) {
        Pattern pattern = Pattern.compile(CHINA_PHONE_NUMBER_PATTERN);
        Matcher matcher = pattern.matcher(phoneNumber);
        return matcher.matches();
    }
}
